//
//  user_myProductViewController.m
//  WorkNest
//
//  Created by Apple on 15/7/28.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "user_myProductViewController.h"
#import "CategoryViewController.h"
#import "ProductDetailsViewController.h"
#import "ApplyRecommendViewController.h"
#import "ProductModel.h"

@interface user_myProductViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray *jsonArray;

@end

@implementation user_myProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];
    [self titleView:@"我的产品"];
    [self initTableView];
    [self requestData];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"type":@"3", @"productType":@"",@"productStyle":@"",@"designerId":@""};
    [RequestData requestProductParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            self.jsonArray = jsonArray;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - loadView
- (void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.rowHeight = 80;
    [self.view addSubview:self.tableView];
}

#pragma mark - tableView Delegate && DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jsonArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentify = @"myProductCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentify];
    }
    ProductModel *model = [self.jsonArray objectAtIndex:indexPath.row];
    UIImageView *coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 60, 60)];
    coverImage.tag = 101;
    [coverImage setImageWithURL:[NSURL URLWithString:[UIUtil containString:model.mainImage]] placeholderImage:[UIImage imageNamed:@"tupian"]];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 10, SCREEN_WIDTH - 120, 20)];
    titleLabel.tag = 102;
    titleLabel.text = model.productName;
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIUtil colorWithHexString:@"#5f5f5f"];
    
    UILabel *volumeLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 30, SCREEN_WIDTH - 120, 20)];
    volumeLabel.tag = 103;
    volumeLabel.text = [NSString stringWithFormat:@"热销:%@件",model.bookedQuantity];
    volumeLabel.textColor = [UIUtil colorWithHexString:@"#aaaaaa"];
    volumeLabel.font = [UIFont systemFontOfSize:11];
    
    UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(110, 50, SCREEN_WIDTH - 120, 20)];
    priceLabel.tag = 104;
    priceLabel.text = [NSString stringWithFormat:@"￥%@",model.quotation];
    priceLabel.textColor = [UIUtil colorWithHexString:@"#fc7b1a"];
    
    [cell.contentView addSubview:coverImage];
    [cell.contentView addSubview:titleLabel];
    [cell.contentView addSubview:volumeLabel];
    [cell.contentView addSubview:priceLabel];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ProductDetailsViewController *view = [[ProductDetailsViewController alloc] init];
    view.productId = ((ProductModel *)[self.jsonArray objectAtIndex:indexPath.row]).productId;
    view.productStatus = ((ProductModel *)[self.jsonArray objectAtIndex:indexPath.row]).productStatus;
    view.ifMyProduct = YES;
    [self.navigationController pushViewController:view animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.tableView.rowHeight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
